﻿using GN.Pay.Core.Exceptions;
using GN.Pay.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace GN.Pay.Core.Services.Impl
{
    /// <summary>
    /// 系统配置服务
    /// </summary>
    class SystemConfigureService : APayService, ISystemConfigureService
    {
        private readonly ISystemConfigureRepository repository;

        public SystemConfigureService(ISystemConfigureRepository repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 获取配置数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public string GetConfigureData(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                PayExceptionUtils.CheckNotNullOrNotWhiteSpace(id, nameof(id));
            }
            var entity = repository.Get(id);
            if (entity == null || !entity.IsEnable)
            {
                return null;
            }
            return entity.ConfigureData;
        }

        private const string MessageSignKey = "MessageSign";

        /// <summary>
        /// 获取或添加消息签名
        /// </summary>
        /// <returns></returns>
        public string GetOrAddMessageSign()
        {
            var entity = repository.Get(MessageSignKey);
            if (entity == null)
            {
                entity = new Entities.SystemConfigure()
                {
                    Id = MessageSignKey,
                    ConfigureData = Guid.NewGuid().ToString("N"),
                    IsEnable = true,
                    OperatorTime = DateTime.Now
                };
                repository.Insert(entity);
            }
            return entity.ConfigureData;
        }
    }
}
